/* ******************************************************************
 * This file is part of the escal project and distributed under the
 * terms of the GNU General Public License Version 3
 * Copyright (C) 2008 Epocher (xijiaosdq@gmail.com)
 * See COPYING file for licensing information.
 *******************************************************************/

#ifndef SCANNER_DIALOG_H
#define SCANNER_DIALOG_H
#include "Python.h"
#include "ui_scanner_dialog.h"
#include "scanner_thread.h"
#include <QDialog>
#include <QUrl>
#include <QBitArray>
#include <QStyledItemDelegate>

class ScannerUrlListDelegate;
class ScannerUrlListModel;
class ScannerThread;
class QFileDialog;
class QDialog;
class ScannerDialog:public QDialog, protected Ui::ScannerDialog{
	Q_OBJECT
public:
	ScannerDialog(QWidget *parent=0);
	~ScannerDialog();
public slots:
	void putUrl(const QString &newUrl);
	void show();
	void accept();
	void reject();
	void switchToRunning();
	void switchToStopped();
	static PyObject *putUrl_py(PyObject *self, PyObject *args);
	static PyObject *keepRunning_py(PyObject *self, PyObject *args);
	static PyObject *getDest_py(PyObject *self, PyObject *args);
	static PyObject *getDepth_py(PyObject *self, PyObject *args);
signals:
	void newTask(QUrl url, QString folderName, QString fileName,
			int threadNum=6);
	void getUrl(const QString &url);
private slots:
	void browseDir();
	void browseScript();
	void chooseAll();
	void unchooseAll();
	void urlChanged(const QString &newUrl);
	void startScan();

private:
	static void init_py_mod();
	bool isKeepRunning(){ return keepRunning; };
	void reset();
	bool keepRunning;
	PyObject *scanner;
	QUrl url;
	QFileDialog *dirDialog;
	QFileDialog *scriptDialog;
	ScannerThread scannerThread;
	ScannerUrlListModel *urlListModel;
	ScannerUrlListDelegate *scannerUrlListDelegate;
};

class ScannerUrlListModel:public QAbstractItemModel{
	Q_OBJECT
public:
	ScannerUrlListModel(QObject *parent=0);
	int columnCount(const QModelIndex &parent=QModelIndex()) const;
	int rowCount(const QModelIndex &parent=QModelIndex()) const;
	Qt::ItemFlags flags(const QModelIndex &index) const;
	QVariant headerData(int column, Qt::Orientation orientation,
			int role) const;
	QModelIndex index(int row, int column,
			const QModelIndex &parent=QModelIndex() ) const;
	QModelIndex parent(const QModelIndex &index) const;
	QVariant data(const QModelIndex&, int) const;
	bool setData(const QModelIndex & index, const QVariant &value,
			int role=Qt::EditRole );
	QStringList getCheckedUrls();
public slots:
	void clear();
	void addUrl(const QString &url);
protected:
	QBitArray checkList;
	QStringList columnHeaders;
	QStringList urlList;
};

class ScannerUrlListDelegate : public QStyledItemDelegate
{
	Q_OBJECT
public:
	ScannerUrlListDelegate(QObject *parent=0):
		QStyledItemDelegate(parent) {};
	void paint(QPainter *painter, const QStyleOptionViewItem &option,
			   const QModelIndex &index ) const;
	QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem
			&option, const QModelIndex & index ) const;
	void setEditorData(QWidget *editor,
			 const QModelIndex &index) const;
	void setModelData(QWidget *editor, QAbstractItemModel *model,
			const QModelIndex & index ) const;
	void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem 
			&option, const QModelIndex &index) const;

};


#endif /* SCANNER_DIALOG_H */
